package flc.ast.dialog;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.stark.translator.util.LanCodeUtil;

import flc.ast.R;
import flc.ast.adapter.LanguageAdapter;
import flc.ast.databinding.DialogLanguageStyleBinding;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.other.LanCode;

/**
 * @Author: XiaoDan
 * @Date: 2023/12/21
 * @Description:
 */
public class LanguageDialog extends BaseSmartDialog<DialogLanguageStyleBinding> {
    public LanCode mCurSelLanCode;
    private LanguageAdapter generalLanAdapter, allLanAdapter;


    public LanguageDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_language_style;
    }

    @Override
    protected void initView(View view) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mDataBinding.rvGeneralLan.setLayoutManager(layoutManager);
        generalLanAdapter = new LanguageAdapter();
        generalLanAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
//                LanCode selLanCode = generalLanAdapter.getItem(position);
                mCurSelLanCode = generalLanAdapter.getItem(position);
                generalLanAdapter.setSelectedLanCode(mCurSelLanCode);
                generalLanAdapter.notifyDataSetChanged();
                allLanAdapter.setSelectedLanCode(null);
                allLanAdapter.notifyDataSetChanged();
                //                selLanCodeAndFinish(selLanCode);
            }
        });
        generalLanAdapter.setNewInstance(LanCodeUtil.getGeneralLanCodes());
        generalLanAdapter.setSelectedLanCode(mCurSelLanCode);
        mDataBinding.rvGeneralLan.setAdapter(generalLanAdapter);

        //rvAllLan
        layoutManager = new LinearLayoutManager(getContext());
        mDataBinding.rvAllLan.setLayoutManager(layoutManager);
        allLanAdapter = new LanguageAdapter();
        allLanAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
//                LanCode lanCode = allLanAdapter.getItem(position);
                mCurSelLanCode = allLanAdapter.getItem(position);

                allLanAdapter.setSelectedLanCode(mCurSelLanCode);
                allLanAdapter.notifyDataSetChanged();

                generalLanAdapter.setSelectedLanCode(null);
                generalLanAdapter.notifyDataSetChanged();

//                if (listener != null) {
//                    dismiss();
//                    listener.clickSelLan(lanCode);
//                }
                //                selLanCodeAndFinish(selLanCode);
            }
        });
        allLanAdapter.setSelectedLanCode(null);
        allLanAdapter.setNewInstance(LanCodeUtil.getAllLanCodes());
        mDataBinding.rvAllLan.setAdapter(allLanAdapter);
        mDataBinding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mDataBinding.ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mDataBinding.ivConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    LanCodeUtil.addLanCode2GeneralHead(mCurSelLanCode);
                    LanCodeUtil.saveGeneralLanCode2File();
                    dismiss();
                    listener.clickSelLan(mCurSelLanCode);
                }
            }
        });
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickSelLan(LanCode lanCode);
    }
}
